import tool from "../../modules/tool"
import store from '../../modules/store'
import config from '../../config'

// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: '',
    username: '',
    phone: '',
    disabled: false,
    show: false,
    sex: 0,
    sexRange: [
      {
        title: '保密'
      },
      {
        title: '男'
      },
      {
        title: '女'
      },
    ],
    birthday: '',
    location: ''
  },

  async onChooseAvatar(e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl
    })
  },

  inputUserName(e) {
    this.setData({
      username: e.detail.value
    })
  },

  inputPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  inputSex(e) {
    this.setData({
      sex: parseInt(e.detail.value)
    })
  },

  inputBirthday(e) {
    this.setData({
      birthday: e.detail.value
    })
  },

  handleInputLocation() {
    if(this.data.disabled)return
    tool.controlSelectLocation((v1, v2) => {
      this.setData({
        location: `${v1}${v2}`
      })
    })
  },

  async handleLogin() {
    let login_re = await tool.requestData('/user/login', {
      code: await tool.getLoginCode()
    })
    if (login_re.status) {
      store.setItem('userData', login_re.data)
      tool.handleShowToast('登录成功', 'success')
      store.setItem('mainCurrentPageIndex', 4)
      wx.navigateBack()
    } else {
      tool.handleShowToast(login_re.content)
    }
  },

  async handleSubmit() {
    if (this.data.avatarUrl == '') {
      tool.handleShowToast('请设置头像')
      return
    }
    if (this.data.username == '') {
      tool.handleShowToast('请输入用户名')
      return
    }
    if (tool.validatePhoneNumber(this.data.phone) == false) {
      tool.handleShowToast('请输入正确的电话号码')
      return
    }
    if (this.data.birthday == '') {
      tool.handleShowToast('请选择生日')
      return
    }
    if ((new Date(this.data.birthday) > new Date())) {
      tool.handleShowToast('生日不能比现在晚')
      return
    }
    let re = await tool.uploadFileToServer(this.data.avatarUrl)
    if (re.status == 0) {
      tool.handleShowToast('头像上传失败', 'error')
      return
    }
    let login_re = await tool.requestData('/user/login', {
      code: await tool.getLoginCode(),
      userInfo: {
        avatar: JSON.parse(re).data,
        username: this.data.username,
        phone: this.data.phone,
        sex: this.data.sex,
        birthday: this.data.birthday
      }
    })
    if (login_re.status) {
      store.setItem('userData', login_re.data)
      tool.handleShowToast('登录成功', 'success')
      store.setItem('mainCurrentPageIndex', 4)
      wx.navigateBack()
    } else {
      tool.handleShowToast(login_re.content)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.setNavigationBarTitle({
      title: '',
    })
    let re = await tool.requestData('/user/info', {
      code: await tool.getLoginCode()
    })
    if (re.data === undefined) {
      wx.setNavigationBarTitle({
        title: '完善个人资料',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '欢迎回来',
      })
      this.setData({
        disabled: true,
        avatarUrl: config.imagePrefix + re.data.avatar,
        username: re.data.username,
        phone: re.data.phone,
        sex: re.data.sex,
        birthday: re.data.birthday,
        location: re.data.location
      })
    }
    this.setData({
      show: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})